package com.settlement.system.controller;

import com.settlement.system.csv.CsvExportBuilder;
import com.settlement.system.csv.CsvExportUtil;
import com.settlement.system.common.enums.ExcelFileType;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.TblWpCheckOutConfig;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblWpCheckOutConfigService;
import com.settlement.system.service.csv.DateSpiltCsvDatas;
import com.alibaba.excel.context.AnalysisContext;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * WP-核查结果配置 前端控制器
 * @author xiaochuan
 * @since 2022-07-29
 */

@Slf4j
@Api(tags = "WP-核查结果配置",description = "WP-核查结果配置")
@RestController
@RequestMapping("/wp/tblWpCheckOutConfig")
public class TblWpCheckOutConfigController extends ExcelController<TblWpCheckOutConfig> {

    @Value("${system.export-root.path}")
    private String exportRootPath;

    @Resource
    private ITblWpCheckOutConfigService tblWpCheckOutConfigService;

    @Override
    public IBaseService init() {
        return tblWpCheckOutConfigService;
    }

    @Override
    public Class<?> getModelClass() {
        return TblWpCheckOutConfig.class;
    }

    @Override
    public String verifyImportObject(TblWpCheckOutConfig detail, AnalysisContext analysisContext) {
        String errorMsg = super.verifyImportObject(detail, analysisContext);
        if(StringUtils.isNotEmpty(errorMsg)){
            return errorMsg;
        }

        try{
            if(StringUtils.isEmpty(detail.getFollowUpDept()) &&
                    StringUtils.isEmpty(detail.getCheckResult()) &&
                    StringUtils.isEmpty(detail.getResponsibleParty()) &&
                            StringUtils.isEmpty(detail.getLossReason())
            ){
                return "跟进部门、核查结果、责任方和损失原因不能同时为空";
            }

            List<TblWpCheckOutConfig> tblWpCheckOutConfigs = tblWpCheckOutConfigService.selectByParams(detail);
            if(!CollectionUtils.isEmpty(tblWpCheckOutConfigs)){
                return "数据已存在";
            }
        } catch (Exception ex) {
            log.error("system error:", ex);
            return "系统异常";
        }

        return null;
    }



    @Override
    protected String validateAllDate(List<TblWpCheckOutConfig> datas) {
        List<String> list = new ArrayList<>();
        for(TblWpCheckOutConfig data : datas){
            String toString = data.toString();
            if(list.contains(toString)){
                return "有两行数据重复";
            }else{
                list.add(toString);
            }
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    protected void saveData(List<TblWpCheckOutConfig> datas) throws ServiceException {
         super.saveData(datas);
    }

    @Override
    public void exportExcelPost(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, Object> params) throws Exception {
        setParamsModel(params);
        String fileName = "WP-核查结果明细";
        String path = exportRootPath + System.currentTimeMillis() + File.separator;
        CsvExportUtil.createCsv()
                .headClass(TblWpCheckOutConfig.class)
                .fileName(fileName)
                .fileType(ExcelFileType.XLSX)
                .model(CsvExportBuilder.MULTI_ZIP_MODEL)
                .parentPath(path)
                .csvDatas(new DateSpiltCsvDatas(tblWpCheckOutConfigService, params))
                .export(request, response);
    }

    @Override
    protected ObjectDataResponse batchDelete(@RequestBody List<TblWpCheckOutConfig> deleteList, HttpServletRequest request) throws ServiceException {
        return super.batchDelete(deleteList, request);
    }
}

